/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Thread;

import vectors.ArrayVector;
import vectors.VectorIn;

/**
 *
 * @author Кирилл
 */
public class Keeper {

    private double min = 0;
    private double max = 1000.0;
    private VectorIn vector;
    private boolean keep = false;
    
    public Keeper(int length) {
        vector = new ArrayVector(length);
    }

    synchronized public void write() throws InterruptedException {
        double val;
        for (int i = 0; i < vector.getSize(); i++) {
            while(keep){
                wait();
            } 
            val = min + Math.random() * (max - min);
            vector.setElement(val, i);
            System.out.println("Write " + val + " to position " + i + "");
            keep = true;
            notifyAll();
        }
}

    synchronized public void read() throws InterruptedException {
        for (int i = 0; i < vector.getSize(); i++) {
           while(!keep){
               wait();
           }
            System.out.println("Read " + vector.getElement(i) + " from position " + i + "");
            keep = false;
            notifyAll();
        }
    }
}